package jmine.tec.report.script;

import java.io.IOException;
import java.io.OutputStream;

import jmine.tec.report.api.Report;
import jmine.tec.report.export.ReportFileExporter;
import jmine.tec.script.ScriptExecutorException;
import jmine.tec.script.persistent.domain.Script;
import bancosys.tec.persist.controller.PersistenceEnabledController;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BeanNotFoundException;

/**
 * Exporter de relatório que delega a funcionalidade para um script.
 * 
 * @author lundberg
 */
public class ReportScriptFileExporter implements ReportFileExporter {

    private PersistenceEnabledController controller;

    private String rendererType;

    private String mnemonicoScript;

    private String logoPath;

    private String fileExtension;

    /**
     * {@inheritDoc}
     */
    public void export(OutputStream outputStream, Report report) throws IOException {
        Script script = this.findScript();
        try {
            script.createContext(ExportReportScriptExecutionContext.class).prepare(outputStream, report).dispatchExecution();
        } catch (ScriptExecutorException e) {
            throw new IllegalStateException("Erro inesperado executando script de renderização de relatório.", e);
        }
    }

    /**
     * @return Script
     */
    private Script findScript() {
        BaseDAO<Script> dao = this.getController().getDAOFactory().getGenericDAO(Script.class);
        try {
            return dao.findByNaturalKey(this.mnemonicoScript);
        } catch (BeanNotFoundException e) {
            throw new IllegalArgumentException("Script de renderização de relatório não encontrado.", e);
        }
    }

    /**
     * {@inheritDoc}
     */
    public String getLogoPath() {
        return this.logoPath;
    }

    /**
     * @param logoPath the logoPath to set
     */
    public void setLogoPath(String logoPath) {
        this.logoPath = logoPath;
    }

    /**
     * {@inheritDoc}
     */
    public String getRendererType() {
        return this.rendererType;
    }

    /**
     * @return the mnemonicoScript
     */
    public String getMnemonicoScript() {
        return this.mnemonicoScript;
    }

    /**
     * @param rendererType the rendererType to set
     */
    public void setRendererType(String rendererType) {
        this.rendererType = rendererType;
    }

    /**
     * @param mnemonicoScript the mnemonicoScript to set
     */
    public void setMnemonicoScript(String mnemonicoScript) {
        this.mnemonicoScript = mnemonicoScript;
    }

    /**
     * @return the controller
     */
    public PersistenceEnabledController getController() {
        return this.controller;
    }

    /**
     * @param controller the controller to set
     */
    public void setController(PersistenceEnabledController controller) {
        this.controller = controller;
    }

    /**
     * @return the fileExtension
     */
    public String getFileExtension() {
        return this.fileExtension;
    }

    /**
     * @param fileExtension the fileExtension to set
     */
    public void setFileExtension(String fileExtension) {
        this.fileExtension = fileExtension;
    }

    /**
     * {@inheritDoc}
     */
    public String getContentType() {
        return "application/octet-stream";
    }

    /**
     * {@inheritDoc}
     * 
     * @see jmine.tec.report.export.ReportFileExporter#getFileSuffix()
     */
    public String getFileSuffix() {
        return this.fileExtension;
    }
}
